In [1]:
%matplotlib inline
In [2]:
# import some modules
import numpy as np
import matplotlib.pyplot as plt
import stingray
In [3]:
# choose style of plots, `seaborn-talk` produce nice big figures
plt.style.use('seaborn-talk')
In [4]:
# Array of timestamps, 10000 bins from 1s to 100s
times = np.linspace(1,100,10000)
# base component of the lightcurve, poisson-like
# the averaged count-rate is 100 counts/bin
noise = np.random.poisson(100,10000)
# time evolution of the frequency of our fake periodic signal
# the frequency changes with a sinusoidal shape around the value 24Hz
freq = 25 + 1.2*np.sin(2*np.pi*times/130)
# Our fake periodic variability with drifting frequency
# the amplitude of this variability is 10% of the base flux
var = 10*np.sin(2*np.pi*freq*times)
# The signal of our lightcurve is equal the base flux plus the variable flux
signal = noise+var
In [5]:
# Create the lightcurve object
lc = stingray.Lightcurve(times, signal)
In [6]:
lc.plot(labels=['Time (s)', 'Counts / bin'])
plt.title('Lightcurve')
Out[6]:
In [7]:
lc.plot(labels=['Time (s)', 'Counts / bin'], axis=[20,23,50,160])
plt.title('Zoomed in Lightcurve')
Out[7]:
In [8]:
ps = stingray.AveragedPowerspectrum(lc, segment_size=3, norm='leahy')
In [9]:
plt.plot(ps.freq, ps.power, label='segment size = {}s \n number of segments = {}'.format(3, int(lc.tseg/3)))
plt.title('Averaged Powerspectrum')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Power')
plt.legend()
Out[9]:
In [10]:
dps = stingray.DynamicalPowerspectrum(lc, segment_size=3)
In [11]:
extent = min(dps.time), max(dps.time), min(dps.freq), max(dps.freq)
plt.imshow(dps.dyn_ps, aspect="auto", origin="lower", vmax=0.001,
interpolation="none", extent=extent)
plt.title('Dynamic Powerspecttrum')
plt.xlabel('Time (s)')
plt.ylabel('Frequency (Hz)')
plt.colorbar(label='Power')
Out[11]:
In [12]:
print("The current frequency resolution is {}".format(dps.df))
Let's rebin to a frequency resolution of 1 Hz and using the average of the power
In [13]:
dps.rebin_frequency(df_new=1.0, method="average")
In [14]:
print("The new frequency resolution is {}".format(dps.df))
Let's see how the Dynamical Powerspectrum looks now
In [15]:
extent = min(dps.time), max(dps.time), min(dps.freq), max(dps.freq)
plt.imshow(dps.dyn_ps, origin="lower", aspect="auto",
interpolation="none", extent=extent)
plt.colorbar()
plt.ylim(15, 30)
Out[15]:
Let's rebin our matrix in the time axis
In [16]:
print("The current time resolution is {}".format(dps.dt))
Let's rebin to a time resolution of 4 s
In [17]:
dps.rebin_time(dt_new=4.0, method="average")
In [18]:
print("The new time resolution is {}".format(dps.dt))
In [19]:
extent = min(dps.time), max(dps.time), min(dps.freq), max(dps.freq)
plt.imshow(dps.dyn_ps, origin="lower", aspect="auto",
interpolation="none", extent=extent)
plt.colorbar()
plt.ylim(15,30)
Out[19]:
In [20]:
# By looking into the maximum power of each segment
max_pos = dps.trace_maximum()
In [21]:
plt.plot(dps.time, dps.freq[max_pos], color='red', alpha=1)
plt.xlabel('Time (s)')
plt.ylabel('Frequency (Hz)')
plt.title('Detected frequency drift')
Out[21]:
In [22]:
extent = min(dps.time), max(dps.time), min(dps.freq), max(dps.freq)
plt.imshow(dps.dyn_ps, aspect="auto", origin="lower", vmax=0.001,
interpolation="none", extent=extent, alpha=0.6)
plt.plot(dps.time, dps.freq[max_pos], color='C3', lw=5, alpha=1, label='drifiting function')
plt.ylim(15,30) # zoom-in around 24 hertz
plt.title('Overlay of Drifting fuction and Dynamic Powerspecttrum')
plt.xlabel('Time (s)')
plt.ylabel('Frequency (Hz)')
plt.colorbar(label='Power')
plt.legend()
Out[22]: